import Vue from 'vue'
import Router from 'vue-router'


import Login from '@/pages/Login'
import Manage from '@/pages/Manage'
import Home from '@/pages/Home'
import ArticleList from '@/pages/article/ArticleList'

import UserList from '@/pages/user/List'
import AddAdmin from '@/pages/user/AddAdmin'
import {getCookie} from "../utils/cookie";
import OutlineList from "../pages/OutlineList";
import OutlineToCheck from "../pages/OutlineToCheck";
import CourseList from "../pages/CourseList";
import AddCourse from "../pages/AddCourse";


// const manage = r => require.ensure([], () => r(require('@/page/Manage')), 'manage');


Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: '/',
            name: '',
            component: Manage,
            redirect: '/home',
            meta: { requireAuth:true},
            children: [
                {
                    path: 'home',
                    component: Home,
                    meta: [],
                },
                {
                    path: 'articleList',
                    name: 'articleList',
                    component: ArticleList,
                    meta: ['添加数据', '添加商铺'],
                },
                {
                    path: 'userList',
                    name: 'userList',
                    component: UserList,
                    meta: ['用户管理', '用户列表'],
                },
                {
                    path: 'addAdmin',
                    name: 'addAdmin',
                    component: AddAdmin,
                    meta: ['用户管理', '添加管理员'],
                },
                {
                    path: 'courseList',
                    name: 'courseList',
                    component: CourseList,
                    meta: ['课程管理', '课程列表'],
                },
                {
                    path: 'addCourse',
                    name: 'addCourse',
                    component: AddCourse,
                    meta: ['课程管理', '添加课程'],
                },
                {
                    path: 'outlineList',
                    name: 'outlineList',
                    component: OutlineList,
                    meta: ['大纲管理', '大纲列表'],
                },
                {
                    path: 'outlineUnCheck',
                    name: 'outlineUnCheck',
                    component: OutlineToCheck,
                    meta: ['大纲管理', '大纲列表'],
                },
            ]
        }
    ]
});
router.beforeEach((to, from, next) => {
    // console.log(to)
    if (to.matched.some(res => {
        // console.log(res);
        return res.meta.requireAuth})) {// 判断是否需要登录权限
        // if (localStorage.getItem('token')) {// 判断是否登录
        if (getCookie('token')) {// 判断是否登录
            next()
        } else {// 没登录则跳转到登录界面
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        }
    } else {
        next()
    }
});
export default router;
